package hidra.io;


import hidra.core.util.Configuration;
import hidra.jmetal.core.Solution;
import hidra.jmetal.core.SolutionSet;

import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Locale;

public class ObjectivesWriter {

	
	private SolutionSet solutionSet;
	
	public ObjectivesWriter(SolutionSet solutionSet) {
		this.solutionSet = solutionSet;
	}
	
	public ObjectivesWriter() {
		
	}
	
	
	public void printObjectivesToFile(String path){
	    try {
	      /* Open the file */
	      FileOutputStream fos   = new FileOutputStream(path)     ;
	      OutputStreamWriter osw = new OutputStreamWriter(fos)    ;
	      BufferedWriter bw      = new BufferedWriter(osw)        ;
	                        
	      for (int i = 0; i < solutionSet.size(); i++) {
	        //if (this.vector[i].getFitness()<1.0) {
	        bw.write(solutionSet.get(i).toString());
	        bw.newLine();
	        //}
	      }
	      
	      /* Close the file */
	      bw.close();
	    }catch (IOException e) {
	      Configuration.logger.severe("Error acceding to the file");
	      e.printStackTrace();
	    }
	  } // printObjectivesToFile
	
	
	private String retrievePattern(int scal){
		String pattern = "";
		
		
		for(int i=0; i < scal-1;i++){
			pattern = pattern + "#";
		}
		
		return "############0.0" + pattern;
	}
	
	
	public void printObjectivesToFile(String path, int scal){
	    try {
	      /* Open the file */
	      FileOutputStream fos   = new FileOutputStream(path)     ;
	      OutputStreamWriter osw = new OutputStreamWriter(fos)    ;
	      BufferedWriter bw      = new BufferedWriter(osw)        ;
	                        
	      for (int i = 0; i < solutionSet.size(); i++) {
	        //if (this.vector[i].getFitness()<1.0) {
	    	//String str = String.format(format, args); 
	    	Solution solution = solutionSet.get(i);
	    	
	    	String aux = "";
	    	NumberFormat nf = NumberFormat.getNumberInstance( Locale.US );
	    	DecimalFormat df = (DecimalFormat)nf;
	    	df.applyPattern(retrievePattern(scal));
	    	
	    	
		    for (int j = 0; j < solution.numberOfObjectives(); j++){
		      aux = aux + 
		      
		      
		      (df).format(solution.getObjective(j))
		      
		      //(new BigDecimal(String.valueOf(solution.getObjective(j) ))).
		      	//	setScale(scal, BigDecimal.ROUND_HALF_UP) 
		      
		      + " ";
		    }
	    	
	        bw.write(aux);
	        bw.newLine();
	        //}
	      }
	      
	      /* Close the file */
	      bw.close();
	    }catch (IOException e) {
	      Configuration.logger.severe("Error acceding to the file");
	      e.printStackTrace();
	    }
	  } // printObjectivesToFile
	
	
	public void printObjectivesToFile(double[][] front, String path){
	    try {	      
	      FileOutputStream fos   = new FileOutputStream(path);
	      OutputStreamWriter osw = new OutputStreamWriter(fos);
	      BufferedWriter bw      = new BufferedWriter(osw);
	                        
	      for (int i = 0; i < front.length ; i++) {
	        String aux="";
	        for(int j=0;j<front[i].length;j++){	    		  	    			  		  
	  		   aux = aux + front[i][j] + " ";
	    	}	    	  
	        bw.write(aux);
	        bw.newLine();	        
	      }	      	      
	      bw.close();
	    }catch (IOException e) {
	      Configuration.logger.severe("Error acceding to the file");
	      e.printStackTrace();
	    }
	  } // printObjectivesToFile
	
	
}
